from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_bootstrap import Bootstrap
from flask_wtf.csrf import CSRFProtect
import os

# 创建Flask应用实例
app = Flask(__name__, template_folder='templates')  # 如果路径不同，修改这里

# 确保项目根目录在Python路径中
from pathlib import Path
import sys
sys.path.append(str(Path(__file__).parent.parent.resolve()))

# 配置
app.config['SECRET_KEY'] = 'your-secret-key-here'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(app.root_path, 'grades.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

# 初始化扩展
db = SQLAlchemy(app)
login_manager = LoginManager(app)
login_manager.login_view = 'login'
bootstrap = Bootstrap(app)
csrf = CSRFProtect(app)

# 导入模型和路由
from app import models, routes
from app.models import User

# 用户加载函数
@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))    